package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {

        userInfoMapper.insert(userInfo);
    }

    @Override
    public void updateById(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public PageResult queryUsers(Integer page, Integer pagesize, Long id, String nickname, String city) {

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();

        if(id!=null){
            queryWrapper.lambda().eq(UserInfo::getId,id);
        }

        if(StringUtils.isNotBlank(nickname)){
            queryWrapper.lambda().like(UserInfo::getNickname,"%"+nickname+"%");
        }
        if(StringUtils.isNotBlank(city)){
            queryWrapper.lambda().like(UserInfo::getCity,"%"+city+"%");
        }

        IPage<UserInfo> iPage = new Page<>(page,pagesize);

        IPage<UserInfo> iPage1 = userInfoMapper.selectPage(iPage, queryWrapper);
        long total = iPage1.getTotal();

        List<UserInfo> list = iPage1.getRecords();

        return new PageResult(page,pagesize,(int)total,list);
    }

}
